package cn.xinfei.xdecision.engine.runner.expressions;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExecutor {
    //对正则取值
    public final static Object getObjFromRegex(Map<String, Object> context, String fieldValue) {
        String result = fieldValue;
        //校验是否使用了字段如果使用了则需要替换为值
        Pattern pattern = Pattern.compile("@[a-zA-Z0-9_\u4e00-\u9fa5()（）-]+@");
        Matcher matcher = pattern.matcher(fieldValue);
        while (matcher.find()) {
            String fieldEn = matcher.group().replace("@", "");
            Object value = context.get(fieldEn);
            String valueStr = "";
            if (value != null) {
                valueStr = value.toString();
            }
            result = result.replace("@" + fieldEn + "@", valueStr);
        }
        return result;
    }

}
